<?php
//新增用户的监听事件
require_once __DIR__."/BaseListener.php";
class ClientAddListener extends BaseListener{
    public function run($params){
        $username = $params['username'];
        $client_id = $params['client_id'];
        $clienttype = $params['clienttype'];
        $company = $params['company'];
        $regdate = sys_get_cid();

        //1 更新通讯录中未注册到平台的联系人
        $sqlstr = "update sys_contract set to_id=$client_id,grouptype=$clienttype where username='$username' and to_id=0";
        $this -> do_execute($sqlstr);

        //更新新加手机号对应的通讯录
        $next_ids = $this -> get_one_bysql("select GROUP_CONCAT(next_id) from sys_contract where username='$username' and next_id<>0");
        if($next_ids){
            $sqlstr = "update sys_contract set client_id=$client_id where id in ($next_ids) ";
            $this -> do_execute($sqlstr);
        }

        //2 更新订单中的用户
        if($clienttype == 1){
            //order_1的卖方
            $sqlstr = "update sys_order_1 set sell_client_id=$client_id where sell_username='$username' and sell_client_id=0 and sell_clienttype=1";
            $this -> do_execute($sqlstr);
        }
        else if($clienttype == 2){
            //order_1的卖方
            $sqlstr = "update sys_order_1 set sell_client_id=$client_id where sell_username='$username' and sell_client_id=0 and sell_clienttype=2";
            $this -> do_execute($sqlstr);
            //order_1的买方
            $sqlstr = "update sys_order_1 set buy_client_id=$client_id where buy_username='$username' and buy_client_id=0 and buy_clienttype=2";
            $this -> do_execute($sqlstr);

            //order_2的卖方
            $sqlstr = "update sys_order_2 set sell_client_id=$client_id where sell_username='$username' and sell_client_id=0 and sell_clienttype=2";
            $this -> do_execute($sqlstr);
        }
        else if($clienttype == 3){
            //更新order_2的买方
            $sqlstr = "update sys_order_2 set buy_client_id=$client_id where buy_username='$username' and buy_client_id=0 and buy_clienttype=3";
            $this -> do_execute($sqlstr);
        }
        else if($clienttype == 4){

        }

        //3 新增店铺
        if(in_array($clienttype, [2,3,4])){
            $code_imgurl = $this -> get_one_bysql("select code_imgurl from sys_client where id=$client_id");
            $shop_sqlstr = "insert into sys_shop set id=$client_id,client_id=$client_id,regdate='$regdate',name='$company',checktype=0,code_imgurl='$code_imgurl'";
            $this -> do_execute($shop_sqlstr);
        }
    }
}